package com.journaldev.fusedlocationprovider;

import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**
 * Created by zhuoyue on 2017/9/20.
 */

public class SecondActivity extends AppCompatActivity implements LocationChangeListener{

    private TextView latLng;
    private GpsLocationManager gpsLocationManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        latLng = (TextView) findViewById(R.id.latLng);
        gpsLocationManager = new GpsLocationManager(this);
        gpsLocationManager.setLocationChangeListener(this);

        if(!gpsLocationManager.checkPlayServicesEnable()) {
            latLng.setText("Please install Google Play Services");
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_location:
                gpsLocationManager.startLocation();
                break;
            case R.id.stop_location:
                gpsLocationManager.stopLocation(false);
                break;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        latLng.setText("Latitude : " + location.getLatitude()+" , Longitude : " + location.getLongitude());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        gpsLocationManager.stopLocation(true);
    }
}
